import { localCache } from '@/utils/cache'
import { createRouter, createWebHashHistory } from 'vue-router'
// 创建路由
const router = createRouter({
  // 路由模式
  history: createWebHashHistory(),
  // 映射关系
  routes: [
    {
      redirect: '/main',
      path: '/'
    },
    {
      name: 'main',
      path: '/main',
      component: () => import('@/views/main/Main.vue')
    },
    {
      name: 'login',
      path: '/login',
      component: () => import('@/views/login/Login.vue')
    },
    {
      name: 'notfound',
      path: '/:pathMatch(.*)',
      component: () => import('@/views/notfound/NotFound.vue')
    }
  ]
})

// 导航守卫
router.beforeEach((to, from) => {
  const token = localCache.getCache('token')
  if (to.path === '/main' && !token) return '/login'
})
// router.afterEach((to, from) => {})

export default router
